export class TextDocumentHandler implements DocumentHandler { private _doc: CodeMirror.Doc = null;
private _text: string = null;
private _editor: { cm: CodeMirror; host: HTMLElement; } = null; private _saveTimer = new Timer();
private _read: (property: string) => string,
private _write: (property: string, content: string) => void) { this._saveTimer.ontick = () => this.save();
if (this._text === null) { this._text = this._doc.getValue();
this._text = this._read(null);
this._doc = new CodeMirror.Doc(this.getText());
CodeMirror.on(this._doc, 'change', (instance, changeObj) => this._onDocChange(changeObj));
return this._getEditor().host;
this._saveTimer.endWaiting();
this._write(null, this.getText());
this._editor = requestEditor();
this._editor.cm.swapDoc(this.getDoc());
private _onDocChange(changeObj: CodeMirror.EditorChange) { var editorCache: { cm: CodeMirror; host: HTMLElement; } [] = []; function requestEditor(): { cm: CodeMirror; host: HTMLElement; } { return editorCache.pop();
var host: HTMLElement = null;
var cm = new CodeMirror(_host => host = _host);
return { cm: cm, host: host }; function returnEditor(editor: { cm: CodeMirror; host: HTMLElement; }) { editorCache.push(editor);